/*
 * Utils.cpp
 *
 *  Created on: 01/10/2010
 *      Author: paulo
 */

#include "Utils.h"

string Utils::IntToString(int valor, int posiciones) {
	string buf;
	int base = 64;

	enum { kMaxDigitos = 35 };
	buf.reserve(kMaxDigitos);

	int cociente = valor;

	do {
		buf += "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"[ abs( cociente % base ) ];
		cociente /= base;
	} while (cociente);


	if ( valor < 0) buf += '-';

	for (int i = buf.length(); i < posiciones ; i++ )
		buf += "0";

	reverse( buf.begin(), buf.end() );
	return buf;
	}

int Utils::StringToInt(string valor) {
	static const string base64_chars =
		"0123456789"
		"abcdefghijklmnopqrstuvwxyz"
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		"+/";
	static const int exp [] = {1, 64, 4096,262144};

	string buf = valor;

	int salida = 0;
	int longVal = valor.length();

	reverse( buf.begin(), buf.end() );
	for(int i = 0; i < longVal; i++)
		salida+=base64_chars.find(buf[i])*exp[i];

	return salida;
	}
